package com.longshine.tools.util;


import java.io.File;   
import java.io.IOException;   
import java.text.SimpleDateFormat;   
import java.util.ArrayList;   
import java.util.Date;   
import java.util.List;   
  
  
/**  
 * 一些公用的工具方法封装  
 *   
 *   
 * @classname PubUtil.java  
 * @author yitianc  
 * @version Revision: 1.1, Date: 2008/05/14 01:43:47  
 * @history Sep 12, 2011 2:11:45 PM  
 *   
 */  
public class DirUtil {   
       
    public static boolean deleteEmptyFolder(String path){   
        File f = new File(path);   
        if(f.exists()&&f.isDirectory()){   
            if(f.list().length==0){   
                String pPath = DirUtil.getParentPath(path);   
                if(pPath!=null&&!"".equals(pPath)){   
                   
                    f.delete();   
                    f = null;   
                    return deleteEmptyFolder(pPath);   
                }else{   
                    return true;   
                }   
            }   
            else{   
                return true;   
            }   
        }   
        else if(!f.exists()){   
            String pPath = DirUtil.getParentPath(path);   
            return deleteEmptyFolder(pPath);   
        }   
        return false;   
    }   
  
    public static String formatFtpPath(String path){   
        if(path.charAt(0)!='/'){   
            return "/"+path;   
        }   
        return path;   
    }   
    public static boolean deleteFile(String path){   
        if(path==null) return false;   
        File f = new File(path);   
        boolean rt = false;   
        if(f.exists()){   
            rt = f.delete();   
            f = null;   
        }   
        return rt;   
    }   
    public static boolean isWindowsPath(String path){   
        return "windows".equals(DirUtil.getPathOs(path));   
    }   
    public static boolean isLinuxPath(String path){   
        return "linux".equals(DirUtil.getPathOs(path));   
    }   
    public static boolean isWindowsOS(){   
        return "windows".equals(DirUtil.getPlatform());   
    }   
    public static boolean isLinuxOS(){   
        return "linux".equals(DirUtil.getPlatform());   
    }   
    public static String toString(Object o){   
        if(o==null) return "";   
        return o.toString();   
    }   
    /**  
     *   
     * 取得文件单位   
     *   
     * @return  
     * @author      YitianC   
     * @history   
     *              YitianC Oct 9, 2011 5:03:07 PM  
     */  
    public static float getFileSizeUnit(){   
        return 1024;   
    }   
    public static boolean isArchiveType(String type){   
        if(type==null) return false;   
        if(type.equals("02")){   
            return true;   
        }   
        return false;   
    }   
    public static boolean isDataType(String type){   
        if(type==null) return false;   
        if(type.equals("01")){   
            return true;   
        }   
        return false;   
    }   
    public static boolean isOtherType(String type){   
        if(type==null) return false;   
        if(type.equals("99")){   
            return true;   
        }   
        return false;   
    }   
  
    /**  
     *   
     * 取得str的上一级路径  
     *   
     * @param str  
     *            路径  
     * @return  
     * @author YitianC  
     * @history YitianC Sep 8, 2011 2:23:52 PM  
     */  
    public static String getParentPath(String str) {   
        if (str.indexOf("\\") >= 0) {   
            return str.substring(0, str.lastIndexOf("\\"));   
        } else if (str.indexOf("/") >= 0) {   
            return str.substring(0, str.lastIndexOf("/"));   
        } else {   
            return null;   
        }   
    }   
  
    /**  
     *   
     * 取得 上一级目录名  
     *   
     * @param str  
     * @return  
     * @author YitianC  
     * @history YitianC Sep 8, 2011 2:24:26 PM  
     */  
    public static String getParentName(String str) {   
        String pPath = getParentPath(str);   
        if (pPath == null) {   
            return null;   
        }   
        return str.substring(pPath.length() + 1, str.length());   
    }   
  
    /**  
     *   
     * 建立path路径中的所有目录  
     *   
     * @param path  
     * @return true表示建立了 fasle:目录存在  
     * @author YitianC  
     * @history YitianC Sep 8, 2011 2:24:48 PM  
     */  
    public static boolean mkdir(String path) {   
        try{   
            if (!isDirExist(path)) {   
                String pPath = getParentPath(path);   
                if(pPath== null){
                	return new File(path).mkdir();  
                }
                if (mkdir(pPath)) {   
                    return new File(path).mkdir();   
                }   
            }   
            return true;   
        }   
        catch(Exception e){   
            return false;   
        }   
  
    }   
  
    /**  
     *   
     * build a folder,  
     *   
     * @param path  
     *            :folder path  
     * @return true:build success ,false:build failed  
     * @author YitianC  
     * @history YitianC Sep 20, 2011 8:37:13 PM  
     */  
    public static boolean buildDir(String path) {   
        try{   
            mkdir(path);   
            File f = new File(path);   
            if (f.exists())   
                return true;   
        }catch(Exception e){   
            return false;   
        }   
        return false;   
    }   
  
    /**  
     *   
     * 判断目录是否存在  
     *   
     * @param path  
     *            要判断的目录路径  
     * @return  
     * @author YitianC  
     * @history YitianC Sep 8, 2011 10:39:44 AM  
     */  
    public static boolean isDirExist(String path) {   
        File file = new File(path);   
        if (!file.exists())   
            return false;   
        if (!file.isDirectory())   
            return false;   
        return true;   
    }   
  
    /**  
     *   
     * 格式化路径  
     *   
     * @param url  
     * @return  
     * @author yitianc  
     * @history yitianc Sep 13, 2011 10:29:09 AM  
     */  
    public static String formatURL(String url) {   
        char[] c = url.toCharArray();   
        boolean flag = false;   
        for (int i = 0; i < c.length; i++) {   
            if (c[i] == '\\') {   
                c[i] = '/';   
                if (i == c.length - 1&&c.length>1)   
                    flag = true;   
            }   
            else if(c[i]=='/'&&i==c.length-1&&c.length>1){   
                flag = true;   
            }   
        }   
        String rt = new String(c);   
        while (flag) {   
            rt = rt.substring(0, rt.length() - 1);   
            if(rt.length()>1){   
                flag = rt.charAt(rt.length()-1)=='/';   
            }   
            else{   
                flag = false;   
            }   
        }   
        return rt;   
    }   
    public static String formatUrlRev(String str){
    	str =formatURL(str);
    	return str.replaceAll("/","\\\\");
    }
  
    /**  
     * 获得当前时间  
     *   
     * @return  
     * @author yitianc  
     * @history yitianc Sep 12, 2011 2:15:14 PM  
     */  
    public static String getCurrentTime() {   
        Date d = new Date();   
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String rt = s.format(d);   
        d = null;   
        s = null;   
        return rt;   
  
    }   
  
    /**  
     *   
     * 取得当前日期  
     *   
     * @return  
     * @author YitianC  
     * @history YitianC Sep 8, 2011 1:47:42 PM  
     */  
    public static String getCurrentDate() {   
        Date d = new Date();   
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");   
        String rt = s.format(d);   
        d = null;   
        s = null;   
        return rt;   
    }   
  
    /**  
     *   
     * 构造文件路径  
     *   
     * @param dirPath  
     *            目录路径  
     * @param fileName  
     *            文件名  
     * @return dirPath 与 fileName的连接  
     * @author yitianc  
     * @history yitianc Sep 13, 2011 10:26:12 AM  
     */  
    public static String dirAddFileName(String dirPath, String fileName) {   
        String rt = dirPath;   
        ;   
        if (dirPath.charAt(dirPath.length() - 1) == '/'  
                || dirPath.charAt(dirPath.length() - 1) == '\\') {   
            rt += fileName;   
        } else {   
            rt += "/" + fileName;   
        }   
        return rt;   
    }   
  
    /**  
     *   
     * according to fileName, get the xml file type;  
     *   
     * @param fileName  
     * @return file type  
     * @author yitianc  
     * @history yitianc Sep 15, 2011 4:12:50 PM  
     */  
    public static String getDateFromTime(String dateTime) {   
        String rt;   
        if(dateTime==null){   
            rt =  getCurrentDate();   
        }   
        else if(dateTime.length()<10){   
            rt = getCurrentDate();   
        }   
        else{   
            rt = dateTime.substring(0, 10);   
            if (rt == null) {   
                rt = getCurrentDate();   
            } else if (rt.equals("")) {   
                rt = getCurrentDate();   
            }   
        }   
        return rt;   
    }   
       
    public static List<String> split(String src,String s){   
        List<String> rt = new ArrayList<String>();   
        int index = src.indexOf(s);   
        int start = 0;   
        while(index>=0){   
            if(start!=index){   
                String r = src.substring(start , index);   
                start = index+1;   
                src = src.substring(start, src.length());   
                rt.add(r);   
                index = src.indexOf(s);   
            }   
            else{   
                start++;   
            }   
               
        }   
  
            rt.add(src);   
           
        return rt;   
    }   
       
    /**  
     *   
     * 格式化时间   
     *   
     * @param time  
     * @param format  
     * @return  
     * @author      YitianC   
     * @history   
     *              YitianC Sep 26, 2011 1:53:51 PM  
     */  
    public static String formatTime(String time, String format) {   
  
  
        if(time == null){   
            return null;   
        }   
        else if(time.equals("")){   
            return null;   
        }   
        try{   
            String oldFormat = null;   
            if(time.length()==14){   
                oldFormat = "yyyyMMddhhmmss";   
            }   
            else if(time.length()==8){   
                oldFormat = "yyyyMMdd";   
            }   
            else if(time.length()==10){   
                oldFormat = "yyyyMMddhh";   
            }   
            else if(time.length()==12){   
                oldFormat = "yyyyMMddhhmm";   
            }   
            else{   
                return null;   
            }   
            SimpleDateFormat s = new SimpleDateFormat(oldFormat);   
            Date d = s.parse(time);   
            s.applyPattern(format);   
               
            String rt = s.format(d);   
            return rt;   
        }catch(Exception e){   
            return null;   
        }   
    }   
  
  
  
  
    /**  
     *   
     * 在目录路径后再加一 级目录  
     *   
     * @param dirPath  
     * @param dirName  
     * @return  
     * @author YitianC  
     * @history YitianC Sep 20, 2011 4:19:12 PM  
     */  
    public static String addDir(String dirPath, String dirName) {   
        dirPath = formatURL(dirPath);   
        dirPath = dirAddFileName(dirPath, dirName);   
        return dirPath;   
    }   
  
    /**  
     *   
     * 根据系统取得工作的根目录，  
     *   
     * @return  
     * @author YitianC  
     * @history YitianC Sep 23, 2011 2:59:12 PM  
     */  
    public static String getBestRootPath() {   
        if (getPlatform().equals("windows")) {   
            File[] f = File.listRoots();   
            if (f.length == 0)   
                return null;   
            if (f.length == 1) {   
                if (f[0].canRead()) {   
                    return formatURL(f[0].getAbsolutePath());   
                } else  
                    return null;   
            }   
            int index = 0;   
            for (int i = 1; i < f.length; i++) {   
  
                String p = f[i].getAbsolutePath();   
                if (p.indexOf("C:") == 0 || p.indexOf("c:") == 0) {   
                    index = i;   
                }   
            }   
            for (int j = index + 1; j < f.length; j++) {   
                String p = f[j].getAbsolutePath();   
                if (f[j].canRead()) {   
                    return formatURL(p);   
                }   
            }   
            if (f[index].canRead())   
                return formatURL(f[index].getAbsolutePath());   
            return null;   
        } else {   
            return "/home/" + System.getProperty("user.name");   
        }   
    }   
  
    /**  
     *   
     * 取得分隔符，它是aheId和orgNo之间的连接符  
     *   
     * @return  
     * @author YitianC  
     * @history YitianC Sep 25, 2011 5:22:53 PM  
     */  
    public static String getSeparator() {   
        return "_";   
    }   
  
    /**  
     *   
     * 根据aheName得到aheId  
     *   
     * @param aheName  
     * @return  
     * @author YitianC  
     * @history YitianC Sep 23, 2011 3:00:50 PM  
     */  
    public static String getAheId(String aheName) {   
        int separator = aheName.indexOf(getSeparator());   
        String aheId = null;   
        String orgNo = null;   
        if (separator >= 0) {   
            String[] s = aheName.split(getSeparator());   
            if (s.length == 1 && separator != 0) {   
                return null;   
            } else if (s.length == 1) {   
                aheId = s[0];   
            } else if (s.length == 2) {   
                aheId = s[1];   
                orgNo = s[0];   
            } else {   
                return null;   
            }   
        } else {   
            return null;   
        }   
        return aheId;   
    }   
  
    /**  
     *   
     * 得到系统名称  
     *   
     * @return  
     * @author YitianC  
     * @history YitianC Sep 23, 2011 3:01:12 PM  
     */  
    public static String getPlatform() {   
        String osName = System.getProperty("os.name");   
        if (osName.indexOf("Windows") >= 0) {   
            return "windows";   
        } else {   
            return "linux";   
        }   
    }   
  
    /**  
     *   
     * 得到调试日志文件路径  
     *   
     * @return  
     * @author YitianC  
     * @history YitianC Sep 23, 2011 3:01:31 PM  
     */  
    public static String getDebugFilePath() {   
        String path = getBestRootPath();   
        path = addDir(path, "listenerDebug");   
        path = dirAddFileName(path, "debug.log");   
        return path;   
    }   
       
    public static String getDebugDir(){   
        String path = getBestRootPath();   
        path = addDir(path,"listenerDebug");   
        return path;   
    }   
  
    /**  
     *   
     * 建立文件   
     *   
     * @param path  
     * @return  
     * @author      YitianC   
     * @history   
     *              YitianC Sep 26, 2011 9:09:05 PM  
     */  
    public static boolean mkfile(String path){   
        File f = new File(path);   
        if(f.exists())return true;   
        if(!buildDir(DirUtil.getParentPath(path))){   
            return false;   
        }   
        File ft = new File(path);   
        try{   
            if(ft.exists()){   
                return true;   
            }   
            return ft.createNewFile();   
        } catch (IOException e) {   
            return false;   
        }   
        finally{   
            f = null;   
            ft =null;   
        }   
    }   
    public static String getPathOs(String path){   
        if(path==null) return null;   
        if(path.indexOf("/")==0){   
            return "linux";   
        }   
        else if(path.indexOf(":")==1){   
            return "windows";   
        }   
        else{   
            return null;   
        }   
    }   
    public static boolean isOsDir(String path){   
        if(path==null) return false;   
        if(DirUtil.getPlatform().equals(getPathOs(path))){   
            return true;   
        }   
        return false;   
    }   
    public static String getLastPathName(String path){   
        String parent = DirUtil.getParentPath(path);   
        return path.substring(parent.length()+1);   
    }   
  
}  
